﻿using System;
using VisionCloud.Core.Model;
using VisionCloud.Domain.Common;

namespace VisionCloud.Domain.Extension
{
    public static class BaseEntityExtension
    {
        public static void SetCreateOperation(this CreateEntity entity)
        {
            var op = OperatorProvider.Get();
            if (op != null)
            {
                entity.CreatedBy = op.YunUserGuid;
            }
            entity.CreatedDate = DateTime.Now;
        }

        /// <summary>
        /// 新增操作填入操作人信息
        /// </summary>
        /// <param name="entity"></param>
        public static void SetOperatorInfoWhenAdd(this BaseEntity entity)
        {
            var op = OperatorProvider.Get();
            if (op != null)
            {
                entity.LastUpdatedBy = entity.CreatedBy = op.YunUserGuid;
            }
            entity.LastUpdatedDate = entity.CreatedDate = DateTime.Now;
            entity.IsDeleted = false;
        }

        /// <summary>
        /// 更新操作填入操作人信息
        /// </summary>
        /// <param name="entity"></param>
        public static void SetOperatorInfoWhenUpdate(this BaseEntity entity)
        {
            var op = OperatorProvider.Get();
            if (op != null)
            {
                entity.LastUpdatedBy = op.YunUserGuid;
            }
            entity.LastUpdatedDate = DateTime.Now;
            entity.IsDeleted = false;
        }

        /// <summary>
        /// 伪删操作除填入操作人信息
        /// </summary>
        /// <param name="entity"></param>
        public static void SetOperatorInfoWhenRemove(this BaseEntity entity)
        {
            var op = OperatorProvider.Get();
            if (op != null)
            {
                entity.LastUpdatedBy = op.YunUserGuid;
            }
            entity.LastUpdatedDate = DateTime.Now;
            entity.IsDeleted = true;
        }
    }
}